/**
 * 
 */
package com.wgo.bpot.server.integrationtest;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.logging.Logger;

/**
 * @author Petter L. H. Eide
 */
public class FileIOHelper {

	private static final Logger log = Logger.getLogger(FileIOHelper.class.getSimpleName());


	public static byte[] readFile( File inputFile ) {
        InputStream in = null;
        byte[] buffer = null;
        try {
            buffer = new byte[(int)inputFile.length()];
            in = new BufferedInputStream(new FileInputStream(inputFile));
            while (in.available() !=0)
            {
                in.read(buffer);
            } 
        } catch (FileNotFoundException e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            log.severe("Unable to locate file " + inputFile.getAbsolutePath() + "\n" + sw.toString());
        } catch (IOException e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            log.severe("Unable to read file " + inputFile.getAbsolutePath() + "\n" + sw.toString());
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw));
                    log.severe("Unable to close file " + inputFile.getAbsolutePath() + "\n" + sw.toString());
                }
            }
        }
		return buffer;
	}

	public static boolean writeToFile ( File outPutFile, byte[] content ) {
        boolean result = false;
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(outPutFile));
            os.write(content);
            result = true;
        } catch (FileNotFoundException e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            log.severe("Unable to locate file " + outPutFile.getAbsolutePath() + "\n" + sw.toString());
        } catch (IOException e) {
            StringWriter sw = new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            log.severe("Unable to write file " + outPutFile.getAbsolutePath() + "\n" + sw.toString());
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    StringWriter sw = new StringWriter();
                    e.printStackTrace(new PrintWriter(sw));
                    log.severe("Unable to close file " + outPutFile.getAbsolutePath() + "\n" + sw.toString());
                }
            }
        }
        return result;

	}
}
